<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product_model extends Model
{
	function Product_model()
	{
		parent::Model();
	}
	function add_record($f_path,$more_pic)
	{
		$att_id=$this->input->post('att_id');
		if(is_array($att_id))
		{
			$att_id=implode(",",$att_id);
		}
		else
		{
			$att_id="";
		}
		$att_id=$att_id?",{$att_id},":"";
		$data=array(
			'title'=>$this->input->post("title"),
			'title_color'=>$this->input->post("title_color"),
			'catalog_id'=>$this->input->post("catalog_id"),
			'market_price'=>$this->input->post("market_price"),
			'seqorder'=>$this->input->post("seqorder"),
			'pNum'=>$this->input->post("pNum"),
			'att_id'=>$att_id,
			'keyword'=>$this->input->post('keyword'),
			'description'=>$this->input->post('description'),
			'is_trash'=>$this->input->post('is_trash')?1:0,
			'summary'=>$this->input->post("summary"),
			'content'=>$this->input->post("content"),
			'create_date'=>time()
		);
		if($f_path['small_pic'])
		{
			$data['small_pic_path']=$f_path['small_pic'];
		}
		else
		{
			$data['small_pic_path']=$this->input->post("pic_path1");
		}
		if($f_path['big_pic'])
		{
			$data['big_pic_path']=$f_path['big_pic'];
		}
		else
		{
			$data['big_pic_path']=$this->input->post("pic_path2");
		}
		$this->db->insert("product",$data);
		if(!empty($more_pic))
		{
			$this->db->select_max('id');
			$query=$this->db->get('product');
			$row=$query->row();
			foreach($more_pic as $pic_row)
			{
				$pic_row['product_id']=$row->id;
				$this->db->insert("product_image",$pic_row);
			}
		}
		return TRUE;
	}
	
	function get_product_att_record()
	{
		$this->db->order_by('seqorder','desc');
		$this->db->order_by('id','desc');
		return $this->db->get("product_att")->result();
	}
	
	function get_record($id)
	{
		$query=$this->db->get_where("product",array('id'=>$id));
		if($query->num_rows()>0)
		{
			return $query->row_array();
		}
	}
	
	function get_pic_record($id)
	{
		$query=$this->db->get_where("product_image",array('product_id'=>$id));
		return $query;
	}
	
	function get_catalog_records()
	{
		return $this->parse_catalog(0);
	}
	function sort_record()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		foreach($rd_id as $row)
		{
			$sort_value=$this->input->post("sort".$row)?(int)$this->input->post("sort".$row):0;
			$this->db->update("product",array('seqorder'=>$sort_value),array('id'=>$row));
		}
		return TRUE;
	}
	
	function save_record($f_path,$more_pic)
	{
		$rd_id=$this->input->post("rd_id");
		$att_id=$this->input->post('att_id');
		if(is_array($att_id))
		{
			$att_id=implode(",",$att_id);
		}
		else
		{
			$att_id="";
		}
		$att_id=$att_id?",{$att_id},":"";
		$data=array(
			'title'=>$this->input->post("title"),
			'title_color'=>$this->input->post("title_color"),
			'catalog_id'=>$this->input->post("catalog_id"),
			'market_price'=>$this->input->post("market_price"),
			'seqorder'=>$this->input->post("seqorder"),
			'pNum'=>$this->input->post("pNum"),
			'att_id'=>$att_id,
			'keyword'=>$this->input->post("keyword"),
			'description'=>$this->input->post("description"),
			'summary'=>$this->input->post("summary"),
			'is_trash'=>$this->input->post('is_trash')?1:0,
			'content'=>$this->input->post("content")
		);
		if($f_path['small_pic'])
		{
			$data['small_pic_path']=$f_path['small_pic'];
		}
		else
		{
			$data['small_pic_path']=$this->input->post("pic_path1");
		}
		if($f_path['big_pic'])
		{
			$data['big_pic_path']=$f_path['big_pic'];
		}
		else
		{
			$data['big_pic_path']=$this->input->post("pic_path2");
		}
		$this->db->update("product",$data,array('id'=>$rd_id));
		if(!empty($more_pic))
		{
			foreach($more_pic as $pic_row)
			{
				$pic_row['product_id']=$rd_id;
				$this->db->insert("product_image",$pic_row);
			}
		}
		return TRUE;
	}
	
	function save_copy_record($f_path,$more_pic)
	{
		$att_id=$this->input->post('att_id');
		if(is_array($att_id))
		{
			$att_id=implode(",",$att_id);
		}
		else
		{
			$att_id="";
		}
		$att_id=$att_id?",{$att_id},":"";
		$data=array(
			'title'=>$this->input->post("title"),
			'title_color'=>$this->input->post("title_color"),
			'catalog_id'=>$this->input->post("catalog_id"),
			'market_price'=>$this->input->post("market_price"),
			'seqorder'=>$this->input->post("seqorder"),
			'pNum'=>$this->input->post("pNum"),
			'att_id'=>$att_id,
			'keyword'=>$this->input->post("keyword"),
			'description'=>$this->input->post("description"),
			'summary'=>$this->input->post("summary"),
			'is_trash'=>$this->input->post('is_trash')?1:0,
			'content'=>$this->input->post("content"),
			'create_date'=>time()
		);
		if($f_path['small_pic'])
		{
			$data['small_pic_path']=$f_path['small_pic'];
		}
		else
		{
			$data['small_pic_path']=$this->input->post("pic_path1");
		}
		if($f_path['big_pic'])
		{
			$data['big_pic_path']=$f_path['big_pic'];
		}
		else
		{
			$data['big_pic_path']=$this->input->post("pic_path2");
		}
		$this->db->insert("product",$data);
		if(!empty($more_pic))
		{
			$this->db->select_max('id');
			$query=$this->db->get('product');
			$row=$query->row();
			foreach($more_pic as $pic_row)
			{
				$pic_row['product_id']=$row->id;
				$this->db->insert("product_image",$pic_row);
			}
		}
		return TRUE;
	}
	
	function put_in_trash()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$this->db->where_in("id",$rd_id);
		$this->db->update('product',array('is_trash'=>1));
		return TRUE;
	}
	
	function trash_back()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$this->db->where_in("id",$rd_id);
		$this->db->update('product',array('is_trash'=>0));
		return TRUE;
	}
	
	function del_record()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$this->db->where_in("id",$rd_id);
		$this->db->delete("product");
		if($this->db->affected_rows())
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	function to_catalog()
	{
		$rd_id=$this->input->post("rd_id");
		if(!is_array($rd_id))
		{
			return FALSE;
		}
		$data=array(
			'catalog_id'=>$this->input->post('to_catalog_id')
		);
		$this->db->where_in("id",$rd_id);
		$this->db->update('product',$data);
		return TRUE;
	}
	
	
	function ajax_del_more_pic($rd_id)
	{
		$this->db->where("id",$rd_id);
		$this->db->delete("product_image");
		if($this->db->affected_rows())
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	function get_trash_num()
	{
		$this->db->where('is_trash',1);
		$this->db->from('product');
		return $this->db->count_all_results();
	}
	
	function parse_catalog($id)
	{
		static $catalog_arr=array();
		$this->db->where('parent_id',$id);
		$this->db->order_by('seqorder','desc');
		$this->db->order_by('id','desc');
		$query=$this->db->get("product_catalog");
		foreach($query->result() as $row)
		{
			$catalog_arr[]=$row;
			if($row->is_has_chd==1)
			{
				$this->parse_catalog($row->id);
			}
		}
		return $catalog_arr;
	}
	
	function image_edit()
	{
		$action=$this->input->post("action");
		$catalog_id=$this->input->post("catalog_id");
		if($action=="thumb")
		{
			$this->db->select('id,big_pic_path');
			$w=$this->input->post("thumb_width")?$this->input->post("thumb_width"):0;
			$h=$this->input->post("thumb_height")?$this->input->post("thumb_height"):0;
			if($catalog_id!="-1")
			{
				$this->db->like('catalog_id',$catalog_id);
			}
			$query=$this->db->get('product');
			$result=$query->result();
			create_thumb($result,$w,$h);
			foreach($result as $row)
			{
				if($row->big_pic_path)
				{
					$this->db->update('product',array('small_pic_path'=>str_replace(".","_thumb.",$row->big_pic_path)),array('id'=>$row->id));
				}
			}
		}
		elseif($action=="watermark")
		{
			$this->db->select('id,big_pic_path');
			if($catalog_id!="-1")
			{
				$this->db->like('catalog_id',$catalog_id);
			}
			$query=$this->db->get('product');
			$result=$query->result();
			create_watermark($result);
			foreach($result as $row)
			{
				$this->db->select('pic_path');
				$this->db->where('product_id',$row->id);
				$query=$this->db->get('product_image');
				foreach($query->result() as $img_row)
				{
					create_watermark($img_row->pic_path);
				}
			}
		}
		return TRUE;
	}
}
?>